var express = require('express')
var bodyParser = require('body-parser')
var multer = require('multer');
var path = require('path');
var upload = multer({
    storage: multer.memoryStorage()
})
var app = express()
var card = require('./card.js').card

var appid = 'wxc1a4f4d8bb9df59a';
var secret = 'a478abe50ae193c2a975863297fd166d';

app.use(bodyParser.json({
    verify: function (req, res, buf, encoding) {
        req.rawBody = buf;
    }
}));
app.use(bodyParser.urlencoded({
    extended: false,
    verify: function (req, res, buf, encoding) {
        req.rawBody = buf;
    }
}));

app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


app.get('/accesstoken', function (req, res) {
    card.getAccessToken(function (response) {
        res.send({ ...response,
            rel: true
        })
    })
})
app.post('/uploadimg', upload.single('logo'), function (req, res) {
    var buffer = req.file.buffer
    card.uploadImg(buffer, function (json) {
        res.send(json)
    })
})

app.post('/formdata', function(req, res){

    res.send(req.body)
})


var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});